package com.bjtime.hprinter.Dialogs;


import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spannable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;
import com.bjtime.hprinter.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  仿MIUI底部弹出对话框,
 *  数据选择对话框
 *  create by liang
 */

public class MIUIInputDialog extends Dialog implements View.OnClickListener {

    private Context context;
    private int layoutResID;      // 布局文件id
    private TextView TitleView;
    private TextView dialog_cancel;
    private TextView dialog_ok;
    private ListView dialog_list;
    private List<Map<String, String>> list;
    private int selectId;
    private String mTitle;
    private String mString;
    private TextView mTextView;
    private TextView mTitleView;
    private int      mMaxLenght = -1;

    public MIUIInputDialog (@NonNull Context context) {
        this (context, R.style.bottom_menu_animation);
    }

    public MIUIInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        this.layoutResID = R.layout.dialog_layout_input;
    }

    /**
     * @param maxLenght 最大字符数量, -1为无限制！0为不允许输入了吧！！！  主要用于 BARCODE_128.
     */
    public void setMaxLenght (int maxLenght) {
        mMaxLenght = maxLenght;
        if (mTextView != null)
            mTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mMaxLenght)});
    }


    public MIUIInputDialog setItemData (String[] items, int mSelectID) {
        if (items.length == 0) {return this;}
        this.list = new ArrayList<>();
        for (int i=0; i<items.length; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", items[i]);
            list.add(map);
        }
        this.selectId = mSelectID;
        return this;
    }

    public MIUIInputDialog setItemData (List items, int selectId) {
        if (items.size() == 0 || items == null) {return this;}
        this.list = new ArrayList<>();
        for (int i=0; i<items.size(); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", items.get(i).toString());
            list.add(map);
        }
        this.selectId = selectId;
        return this;
    }

    @Override
    public void onClick(View v) {
        if (v == dialog_ok) {
            if (mButtonClickListener!=null)
                mButtonClickListener.onOk(v);

            this.dismiss();
        }
        if (v == dialog_cancel) {
            if (mButtonClickListener!=null)
                mButtonClickListener.onCancel(v);
            this.dismiss();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResID);

        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_menu_animation); // 添加动画效果
        WindowManager windowManager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        lp.width = (int) (metrics.widthPixels * 0.95);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        lp.dimAmount = 0.5f;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);// 点击Dialog外部消失

        mTextView = findViewById(R.id.ipt_dia_text_view);
        mTitleView = findViewById(R.id.ipt_dia_title);
        mTextView.setText(mString);
        mTitleView.setText(mTitle);

        dialog_cancel = findViewById(R.id.ipt_dia_cancel);
        dialog_ok = findViewById(R.id.ipt_dia_ok);

        dialog_cancel.setOnClickListener(this);
        dialog_ok.setOnClickListener(this);

        mTextView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(mMaxLenght)});
    }


    public String getString () {
        return ((TextView)findViewById(R.id.ipt_dia_text_view)).getText ().toString();
    }

    public void setString (String s) {
        if (mTextView != null)
            mTextView.setText(s);
        mString = s;
    }



    @Override
    public void setTitle(@Nullable CharSequence title) {
        super.setTitle(title);
        if (mTitleView != null)
            mTitleView.setText(title);
        mTitle = title.toString ();
    }

    public interface OnButtonClick {
        void onOk(View v);
        void onCancel(View v);
    }

    private OnButtonClick mButtonClickListener;
    public void setOnButtonClickListener (OnButtonClick listener) {
        mButtonClickListener = listener;
    }


}
